<?php
class Signup extends CI_Controller {
	var $objLang, $context;

	 function __construct()
    {
		 global $data;
        // Call the Model constructor
        parent::__construct();
		$this->load->library('form_validation');
		$this->load->helper('cookie');

		  $config = $this->config->item('db_config');
		$this->load->model('Dbmodel', '', $config);

		$objLang = $this->Dbmodel->select_where('language', '', 'lang_name');
		
		if ( $objLang )
		{
			$this->objLang = $objLang;
		}
		header('Content-type: text/html; charset=utf-8');

		if ( get_cookie('lang') == '' )
			$default_lang = $this->objLang[0]->lang_name;
		else
			$default_lang = get_cookie('lang');

		$string_lang = file_get_contents('http://'.$_SERVER['HTTP_HOST'].'/hotelbooking/lang/'.$default_lang.'.xml');

		$xml_lang = simplexml_load_string($string_lang);


		$this->context = array(
				'lang_name' => strip_tags($xml_lang->name),
				'title' =>strip_tags($xml_lang->title) ,
				'description' => strip_tags($xml_lang->desc) ,
				'txt_signup' => strip_tags($xml_lang->text_signup) ,
				'txt_signup_hotel' => strip_tags($xml_lang->text_signup_hotel) ,
				
			);

    }
	function index()
	{
		  $data = array(
               'customer' => '/customer',
               'hotel' => '/hotel',
			  	'registed' => false,
			    'user_dup' => '',
			   'typeUser' => ''
          ); 

		
		$data['lang'] = $this->objLang;

	
		$data['context'] = $this->context;
		


		$this->load->view('signupview', $data);

	}
	function customer()
	{
		$data = array(
               'customer' => '/customer',
               'hotel' => '/hotel',
				'registed' => false,
				'user_dup' => '',
				'typeUser' => 'Customer'
          ); 
		$data['lang'] = $this->objLang;
		$data['context'] = $this->context;
		$this->load->view('signupview', $data);
	}
	function hotel()
	{
		$data = array(
               'customer' => '/customer',
               'hotel' => '/hotel',
				'registed' => false,
				'user_dup' => '',
				'typeUser' => 'Hotel'
          ); 
		$data['lang'] = $this->objLang;
		$data['context'] = $this->context;
		$this->load->view('signupview', $data);
	}
	function register()
	{

		$config = $this->config->item('db_config');

		$this->load->model('Dbmodel', '', $config);

		if ( $this->input->post('typeUser') == 'Hotel' )
			$hotelName = $this->input->post('HotelName');
		else
			$hotelName = 'none';

		
		$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[12]|xss_clean');
		$this->form_validation->set_rules('password', 'Password', 'trim|required|matches[passconf]');
		$this->form_validation->set_rules('passconf', 'Password Confirmation', 'trim|required');
		$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');

		$data = array(
               'customer' => '/customer',
               'hotel' => '/hotel',
				'registed' => false,
				'user_dup' => '',
				'typeUser' => 'Customer'
          ); 

		if ($this->form_validation->run() == FALSE)
		{
			$this->load->view('signupview', $data);
			
		}
		else
		{


				$this->load->library('encrypt');

				$username = $this->security->xss_clean($this->input->post('username'));

		//		print_r($username);
		//		die;

				$cond_member = array('mem_user' => $username);
				$res_member = $this->Dbmodel->select_where('member', $cond_member, 'mem_user');

				if ( $res_member )
				{
					
					$data['user_dup'] = 'member name use already !!!';
					$this->load->view('signupview', $data);
				}
				else
				{

						$insertData  = array(
							'mem_user' => $username,
						   'mem_fname' => $this->security->xss_clean($this->input->post('Fname')) ,
						   'mem_lname' => $this->security->xss_clean($this->input->post('Lname')) ,
						   'mem_pass' => $this->encrypt->encode($this->security->xss_clean($this->input->post('password'))) ,
					//	   'mem_repass' => $this->encrypt->encode($this->input->post('RePassword')) ,
						   'mem_email' => $this->security->xss_clean($this->input->post('email')) ,
							'mem_hotel' => $hotelName ,
						   'mem_role' => $this->input->post('typeUser')
						);



						$this->Dbmodel->insert_entry('member', $insertData);

						$data = array(
					  //         'customer' => '/customer',
				  //             'hotel' => '/hotel',
								'registed' => true
						  ); 

				//		$this->load->view('signupview', $data);
						header("location:http://".$_SERVER['HTTP_HOST']."/hotelbooking/index.php/login");
				}
		}
	}
}
?>